package com.ruoyi.project.xkx.school.controller;

import java.util.List;
import java.util.Map;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.xkx.school.domain.XkxClass;
import com.ruoyi.project.xkx.school.service.IXkxClassService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 所有班级Controller
 *
 * @author ..
 * @date 2021-05-11
 */
@RestController
@RequestMapping("/school/class")
public class XkxClassController extends BaseController {
  @Autowired
  private IXkxClassService xkxClassService;

  /**
   * 查询校区班级
   */
  @GetMapping("/getSchoolClassStudent/{schoolId}")
  public AjaxResult getSchoolClassStudent(@PathVariable Integer schoolId, Integer classId) {
    List<XkxClass> schoolClassStudent = xkxClassService.getSchoolClassStudent(schoolId, classId);
    return AjaxResult.success("查询校区班级成功", schoolClassStudent);
  }

  /**
   * 班级消课
   */
  @PutMapping("/classAttendance/{classId}")
  public AjaxResult classAttendance(@PathVariable Integer classId) {
    Map map = xkxClassService.classAttendance(classId);
    return AjaxResult.success("消课成功", map);
  }

  /**
   * 查询校区班级
   */
  @GetMapping("/schoolClass/{schoolId}")
  public AjaxResult getSchoolForClass(@PathVariable Integer schoolId) {

    List<XkxClass> school_id = xkxClassService.list(new QueryWrapper<XkxClass>().eq("school_id", schoolId));

    return AjaxResult.success("查询校区班级成功", school_id);
  }

  /**
   * 查询所有班级列表
   */
  @PreAuthorize("@ss.hasPermi('school:class:list')")
  @GetMapping("/list")
  public TableDataInfo list(XkxClass xkxClass) {
    startPage();
    List<XkxClass> list = xkxClassService.selectXkxClassList(xkxClass);
    return getDataTable(list);
  }

  /**
   * 导出所有班级列表
   */
  @PreAuthorize("@ss.hasPermi('school:class:export')")
  @Log(title = "所有班级", businessType = BusinessType.EXPORT)
  @GetMapping("/export")
  public AjaxResult export(XkxClass xkxClass) {
    List<XkxClass> list = xkxClassService.selectXkxClassList(xkxClass);
    ExcelUtil<XkxClass> util = new ExcelUtil<XkxClass>(XkxClass.class);
    return util.exportExcel(list, "所有班级数据");
  }

  /**
   * 获取所有班级详细信息
   */
  @PreAuthorize("@ss.hasPermi('school:class:query')")
  @GetMapping(value = "/{classId}")
  public AjaxResult getInfo(@PathVariable("classId") Integer classId) {
    return AjaxResult.success(xkxClassService.selectXkxClassById(classId));
  }

  /**
   * 新增班级
   */
  @PreAuthorize("@ss.hasPermi('school:class:add')")
  @Log(title = "新增班级", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@RequestBody XkxClass xkxClass) {
    return toAjax(xkxClassService.insertXkxClass(xkxClass));
  }

  /**
   * 修改班级
   */
  @PreAuthorize("@ss.hasPermi('school:class:edit')")
  @Log(title = "所有班级", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@RequestBody XkxClass xkxClass) {
    return toAjax(xkxClassService.updateXkxClass(xkxClass));
  }

  /**
   * 删除所有班级
   */
  @PreAuthorize("@ss.hasPermi('school:class:remove')")
  @Log(title = "所有班级", businessType = BusinessType.DELETE)
  @DeleteMapping("/{classIds}")
  public AjaxResult remove(@PathVariable Integer[] classIds) {
    return toAjax(xkxClassService.deleteXkxClassByIds(classIds));
  }

  @GetMapping("/getSchoolClass/{classId}")
  public AjaxResult getSchoolClass(@PathVariable Integer classId) {
    XkxClass one = xkxClassService.getOne(new QueryWrapper<XkxClass>()
        .select("school_id")
        .eq("class_id", classId));
    List<XkxClass> schoolList = null;
    if (one != null) {
      Integer schoolId = one.getSchoolId();
      schoolList = xkxClassService.list(new QueryWrapper<XkxClass>().eq("school_id", schoolId));
    }
    return AjaxResult.success(schoolList);
  }
  @GetMapping("/getClassTeacherId/{teacherId}")
  public AjaxResult getClassByTeacherId(@PathVariable Integer teacherId) {
    List<XkxClass> listClass = xkxClassService.getListClassByTeacherId(teacherId);
    return AjaxResult.success(listClass);
  }

  @GetMapping("/getClassInfo/{classId}")
  public AjaxResult getClassInfo(@PathVariable Integer classId) {
    XkxClass classInfo = xkxClassService.getClassInfo(classId);
    return AjaxResult.success(classInfo);
  }

}
